package org.springblade.tms.module.exceptioncosttype.base;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.logging.log4j.util.Strings;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.module.exceptioncosttype.entity.TMExceptionCostType;
import org.springblade.tms.module.exceptioncosttype.service.IExceptionCostTypeService;
import org.springblade.tms.module.exceptioncosttype.vo.CostType;
import org.springblade.tms.module.exceptioncosttype.vo.TMExceptionCostTypeVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.exceptioncosttype.base
 * @className: ExceptionMoldTypeService
 * @author: yr
 * @description: 异常费用类型服务
 * @date: 2022/4/20 17:48
 */
@Service
public class ExceptionMoldTypeService {
	@Resource
	private IExceptionCostTypeService exceptionCostTypeService;

	/**
	 * @param tmExceptionCostType:
	 * @param query:
	 * @return R<IPage < ExceptionCostType>>
	 * @author yr
	 * @description 异常费用类型列表查询
	 * @date 2022/4/20 18:10
	 */
	public R<IPage<CostType>> list(TMExceptionCostType tmExceptionCostType, Query query) {
		IPage page = exceptionCostTypeService.page(Condition.getPage(query),
			Condition.getQueryWrapper(new TMExceptionCostType()).lambda()
				.like(Strings.isNotBlank(tmExceptionCostType.getOrderType()),TMExceptionCostType::getOrderType, tmExceptionCostType.getOrderType())
				.like(Strings.isNotBlank(tmExceptionCostType.getOrderTypeDesc()),TMExceptionCostType::getOrderTypeDesc, tmExceptionCostType.getOrderTypeDesc())
				.groupBy(TMExceptionCostType::getOrderType).orderByDesc(TMExceptionCostType::getCreateTime,TMExceptionCostType::getId));
		List<TMExceptionCostType> exceptionCostTypes = exceptionCostTypeService.lambdaQuery().list();
		Map<String, List<TMExceptionCostType>> exceptionCostMap = exceptionCostTypes.stream().collect(Collectors.groupingBy(TMExceptionCostType::getOrderType));
		List<CostType> costTypeList = BeanUtil.copy(page.getRecords(), CostType.class);
		costTypeList.forEach(costType -> costType.setNum(exceptionCostMap.get(costType.getOrderType()).size()));
		page.setRecords(costTypeList);
		return R.data(page);
	}

	/**
	 * @param exceptionCostTypes:
	 * @return R
	 * @author yr
	 * @description 新增或删除异常费用类型
	 * @date 2022/4/22 10:58
	 */
	public R saveOrUpdate(List<TMExceptionCostTypeVO> exceptionCostTypes) {
		//判断新增还是修改
		List<TMExceptionCostType> tmExceptionCostTypes = BeanUtil.copy(exceptionCostTypes, TMExceptionCostType.class);
		String orderType = exceptionCostTypes.get(0).getOrderType();
		List<TMExceptionCostType> exceptionCostTypeList = exceptionCostTypeService.lambdaQuery().eq(TMExceptionCostType::getOrderType, orderType).list();
		//删除部分
		exceptionCostTypeList.removeAll(tmExceptionCostTypes);
		//新增或修改部分
		tmExceptionCostTypes.removeAll(exceptionCostTypeList);
		List<Long> ids = exceptionCostTypeList.stream().map(TMExceptionCostType::getId).collect(Collectors.toList());
		if (!ids.isEmpty()) {
			exceptionCostTypeService.deleteLogic(ids);
		}
		exceptionCostTypeService.saveOrUpdateBatch(tmExceptionCostTypes);
		return R.status(true);
	}
}
